<template>
	<view class="page-wrap" :data-theme="theme" :class="theme" :style="globalColor.style">
		<!-- <HeadCustom title="备考生" :isBlur="true" :isAppBtn="false" :isHome="false"></HeadCustom> -->
		<header-custom title="备考生" :isBlur="true" :isAppBtn="false" :isHome="false"/>
		<view class="wrap fboxCol Ycenter">
			<view class="cell cjcx fboxCol" @click="goPage('score')">
				<view class="tit font32 fwb">成绩查询</view>
				<view class="con font24">康德联考成绩、排名查询</view>
				<image class="pic" src="https://ebc-prod.cdn.benwunet.com/uni-static/img3.5.0/bks/pic1.webp"></image>
			</view>
			<view class="cell kszl fboxCol" @click="goPage('datum')">
				<view class="tit font32 fwb">考试资料</view>
				<view class="con font24">专题、题库</view>
				<image class="pic" src="https://ebc-prod.cdn.benwunet.com/uni-static/img3.5.0/bks/pic2.webp"></image>
			</view>
			<view class="cell ssrz fboxCol" @click="goPage('certification')">
				<view class="tit font32 fwb">学生信息</view>
				<view class="con font24">
					<text class="mr24">姓名：{{student.studentName}}</text> <text class="mr24">年级：{{student.gradeName}}</text>
					<text class="mr24">学校：{{student.unitName}}</text>
				</view>
				<!-- <image class="pic" src="https://ebc-prod.cdn.benwunet.com/uni-static/img3.5.0/bks/pic2.webp"></image> -->
			</view>
			<view class="apply fboxRow Ycenter Xcenter" @click="goPage('certification')"><text
					class="color-00C15D font46 fwb">修改认证信息，点这里</text></view>
		</view>
	</view>
</template>

<script>
	import {
		getRequest,
		throttle
	} from '@/static/js/common.js';
	import {
		utf8to16,
		base64decode
	} from '@/static/js/Base64.js';
	import {
		tripartitLogin,
		tripartitLoginNew,
		studentInfo
	} from '@/api/bks.index.js'
	import {
		isBandingWeChant
	} from '@/api/mine.js'
	import {
		userInfo
	} from '@/api/bks.mine.js'
	import headerCustom from '@/pagesBKS/components/header-custom.vue';
	export default {
		components: {headerCustom},
		data() {
			return {
				theme: 'bks-theme',
				globalColor: getApp().globalData.globalColor,
				mobile: '',
				unionId: '',
				student: {},
				access_token: '',
				isApp: 0
			}
		},
		onLoad(option) {
			if (option && option.benwu) {
				const benwu = utf8to16(base64decode(option.benwu));
				const token = {
					access_token: getRequest(benwu, 'token'),
					statusBarHeight: getRequest(benwu, 'statusBarHeight'),
					phone: getRequest(benwu, 'phone'),
				};
				this.mobile = token.phone
				getApp().globalData.statusBarHeight = token.statusBarHeight;
				uni.setStorageSync('bw_params', JSON.stringify(token));
			} else {
				const { bw_user_temp } = getApp().getBWData(['bw_user_temp'])
				this.mobile = bw_user_temp.mobile
			}
			if (option && option.isApp == 1) {
				this.isApp = option.isApp
			}
			// const bw_bks_params = JSON.parse(sessionStorage.getItem("bw_bks_params") || '{}');
			// if (bw_bks_params && !bw_bks_params.access_token) {
			// 	uni.removeStorageSync('bw_bks_params')
			// 	uni.removeStorageSync('bw_bks_student')
			// 	uni.removeStorageSync('bw_bks_user')
			// 	// 判断是否登陆，如果登陆过的不再进入登陆
			// 	this.getWeChat()
			// }
			this.getInit()
		},
		mounted() {
			window.appCallback = this.appCallback;
		},
		methods: {
			// 初始化
			getInit(){
				//判断缓存里面是否存在登录信息，有直接取缓存登录信息
				const {bw_bks_params, bw_bks_student} = getApp().getBWData(['bw_bks_params', 'bw_bks_student'])
				if(bw_bks_params && bw_bks_params.unionId){
					sessionStorage.setItem('bw_bks_params', JSON.stringify(bw_bks_params))
					if (bw_bks_student && bw_bks_student.unitId) {
						this.student = bw_bks_student
					}
				}else{
					// 没有登录信息那么就请求登录
					this.getWeChat()
				}
			},
			// 获取是否绑定微信
			getWeChat: throttle(function() {
				uni.showLoading({title: '加载中',mask: true})
				isBandingWeChant().then(res => {
					if (res.data) {
						this.unionId = res.data
						const {bw_bks_params, bw_bks_student} = getApp().getBWData(['bw_bks_params', 'bw_bks_student'])
						if(bw_bks_params && bw_bks_params.unionId && res.data == bw_bks_params.unionId && bw_bks_student.unitId){
							sessionStorage.setItem('bw_bks_params', JSON.stringify(bw_bks_params))
							if (bw_bks_student && bw_bks_student.unitId) {
								this.student = bw_bks_student
							}
							uni.hideLoading()
							// 当前登录用户unionId跟请求接口unionId一致不请求登录，取缓存内容
						}else{
							this.goLogin()
						}
					} else {
						uni.hideLoading()
						this.modalWeChat()
						uni.removeStorageSync('bw_bks_params')
						uni.removeStorageSync('bw_bks_student')
						uni.removeStorageSync('bw_bks_user')
					}
				}).catch(err=>{
					// uni.removeStorageSync('bw_bks_params')
					// uni.removeStorageSync('bw_bks_student')
					// uni.removeStorageSync('bw_bks_user')
					uni.hideLoading()
				})
			}, 1000),
			// 提示绑定微信
			modalWeChat() {
				if(this.isApp != 1){
					uni.showToast({
						title: '请先前往APP，绑定微信！',
						icon: 'none'
					})
					return false
				}
				uni.showModal({
					title: '温馨提示',
					content: '亲查成绩、查排名、查答案，请先绑定微信哦！',
					cancelText: '取消',
					confirmText: '去绑定',
					confirmColor: '#00C15D',
					success: (res) => {
						if (res.confirm) {
							const that = this
							const data = {
								type: 'bindWeChat'
							};
							this.$bridge.callHandler('appHandler', data, (res) => {
								console.log('appHandler', '返回', res)
								that.appCallback(res)
							})
						}
					}
				})
			},
			// App回调
			appCallback(e) {
				if (e.unionId) {
					this.unionId = e.unionId
					this.goLogin()
				}
			},
			// 自动登陆
			goLogin: throttle(function(type) {
				tripartitLoginNew({
					mobile: this.mobile,
					unionId: this.unionId
				}).then(res => {
					if(res.data.accessToken){
						this.access_token = res.data.accessToken.access_token
						res.data.accessToken.unionId = this.unionId
						// 路由存在问题对数据进行存储
						sessionStorage.setItem('bw_bks_params', JSON.stringify(res.data.accessToken))
						uni.setStorageSync('bw_bks_params', res.data.accessToken)
						uni.setStorageSync('bw_bks_user', res.data)
						if (res.data.authInfoVO) {
							this.student = res.data.authInfoVO
							uni.setStorageSync('bw_bks_student', res.data.authInfoVO)
							if(type){
								this.goPage(type)
							}
						} else {
							this.$pageTo('/bks/mine/certification/index')
						}
						// this.getInfo(type)
					// }
					// if (res.data.access_token) {
					// 	this.access_token = res.data.access_token
					// 	res.data.unionId = this.unionId
					// 	// 路由存在问题对数据进行存储
					// 	sessionStorage.setItem('bw_bks_params', JSON.stringify(res.data))
					// 	uni.setStorageSync('bw_bks_params', res.data)
					// 	this.getInfo(type)
					} else {
						uni.showToast({
							icon: 'none',
							title: '服务器繁忙'
						})
					}
				}).finally(err=>{
					uni.hideLoading()
				})
			}, 1000),
			// 获取用户信息和学生信息
			getInfo(type) {
				// 学生信息
				studentInfo().then(res => {
					if (res.data) {
						this.student = res.data
						uni.setStorageSync('bw_bks_student', res.data)
						if(type){
							this.goPage(type)
						}
					} else {
						this.$pageTo('/bks/mine/certification/index')
					}
				})
				// 用户信息
				userInfo().then(res => {
					if (res.data) {
						uni.setStorageSync('bw_bks_user', res.data)
					}
				})
			},
			// 跳转页面
			goPage(type) {
				const bw_bks_params = uni.getStorageSync('bw_bks_params') || {}
				const bw_bks_student = uni.getStorageSync('bw_bks_student') || {}
				// 判断是否绑定微信
				if (bw_bks_params.unionId) {
					// 判断是否登陆
					if (bw_bks_params.access_token) {
						if (type == 'certification') {
							this.$pageTo('/bks/mine/certification/index')
						} else {
							// 学生认证是否填写
							if (bw_bks_student.studentName) {
								if (type == 'datum') {
									this.$pageTo('/bks/datum/index/index')
								} else if(type == 'score'){
									this.$pageTo('/bks/volunteer/myScore/assign/index')
								}
							} else {
								this.$pageTo('/bks/mine/certification/index')
							}
						}
					} else {
						this.goLogin(type)
					}
				} else {
					// this.modalWeChat()
					this.getWeChat()
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page-wrap{ min-height: calc(100vh); background-color: #fff;}
	.color-00C15D{color: #00C15D}
	.wrap {
		padding: 50rpx 0;
	}
	.mr24{margin-right: 24rpx;white-space: nowrap;}

	.cell {
		margin-bottom: 40rpx;
		padding: 50rpx 0 0 30rpx;
		width: 670rpx;
		position: relative;
		height: 180rpx;
		background: linear-gradient(177deg, #ECEDFF 0%, #FFFFFF 80%);
		box-shadow: 0rpx 8rpx 16rpx 0rpx rgba(207, 215, 222, 0.28);
		border-radius: 12rpx;
		color: #111018
	}

	.cell .con {
		margin-top: 16rpx;
		line-height: 34rpx;
		color: #929297
	}

	.cjcx .pic {
		position: absolute;
		right: 20rpx;
		top: -26rpx;
		width: 194rpx;
		height: 196rpx;
	}

	.kszl {
		background: linear-gradient(177deg, #FFEBF5 0%, #FFFFFF 100%);
	}

	.kszl .pic {
		position: absolute;
		right: 35rpx;
		top: -13rpx;
		width: 181rpx;
		height: 175rpx;
	}

	.apply {
		position: fixed;
		left: 0;
		bottom: 60rpx;
		width: 100%;
		text-align: center;
		height: 80rpx;
	}

	.ssrz {
		background: linear-gradient(177deg, #e1f6e5 0%, #FFFFFF 100%);
	}
</style>